import 'dart:io';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

typedef ReturnButtonCallback<T> = T Function();

/*
 * 返回按钮
 */
class ReturnButton extends StatelessWidget {
  double? left;
  double? top;
  double? width;
  double? height;
  String? image;
  String? imagePress;
  bool? status;
  ReturnButtonCallback? callback;

  ReturnButton({
    super.key,
    this.left,
    this.top,
    this.width,
    this.height,
    this.image,
    this.imagePress,
    this.callback,
    this.status = false,
  });

  @override
  Widget build(BuildContext context) {
    double pleft = 44, ptop = 44, pwidth = 60, pheight = 60;
    if (Platform.isIOS) {
      double screenHeight = MediaQuery.of(context).size.height;
      double screenWidth = MediaQuery.of(context).size.width;
      double w = max<double>(screenHeight, screenWidth);
      if (w > 1000) {
        pleft = 10;
        ptop = 10;
      }
    }
    return Positioned(
      left: left ?? ScreenUtil().setHeight(pleft),
      top: top ?? ScreenUtil().setHeight(ptop),
      width: width ?? ScreenUtil().setHeight(pwidth),
      height: height ?? ScreenUtil().setHeight(pheight),
      child: Container(
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage(backgroundImage()),
          ),
        ),
        child: GestureDetector(
          onTap: () {
            var obj;
            if (callback != null) {
              obj = callback!();
            }
            if (obj != null) {
              Navigator.of(context).pop(obj);
            } else {
              Navigator.of(context).pop();
            }
          },
        ),
      ),
    );
  }

  String backgroundImage() {
    return status != null && status!
        ? (imagePress ?? 'assets/return_btn.png')
        : (image ?? 'assets/return_btn.png');
  }
}
